/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Validation;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author sunwei
 */
public class EmailValidator implements Validator{

    
    
    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        
        String enteredEmail = (String)value;
        
        Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
        
        Matcher m = p.matcher(enteredEmail);
        
        boolean matchFound = m.matches();
        
        if(enteredEmail.equals("")){
            
        }
        
        else if (!matchFound) {
        FacesMessage message = new FacesMessage();
        message.setDetail("Email is not in the format yourname@yourdomain.com");
        message.setSummary("Email is not in the format yourname@yourdomain.com");
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        throw new ValidatorException(message);
        }
        else{};
        
       
    
        
    }
        
        
}
